package com.hxkj.controller;

import com.hxkj.domain.annotation.RequestLimit;
import com.hxkj.domain.enums.MessageExceptionEnum;
import com.hxkj.domain.po.CommonMessage;
import com.hxkj.service.MessageService;
import com.platform.common.domain.result.ResultMessageStruct;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 短信控制器
 * <p>
 * Create by chenwei on 2018/7/9 14:14
 */
@RestController
@Slf4j
public class MessageController {

    @Autowired
    private MessageService messageService;

    /**
     * 发送短信
     *
     * @param commonMessage
     * @return
     */
    @RequestLimit
    @RequestMapping(value = "/message/send", consumes = "application/json;charset=UTF-8")
    public ResultMessageStruct sendMessage(@RequestBody CommonMessage commonMessage) {
        ResultMessageStruct resultMessageStruct = new ResultMessageStruct();
        if (commonMessage.getLimitTag() == 1) {
            resultMessageStruct.setCode(400);
            resultMessageStruct.setMessage("请求频繁");
            return resultMessageStruct;
        }
        if (StringUtils.isAnyEmpty(commonMessage.getMessage(),commonMessage.getMessageType(),commonMessage.getServiceId())){
            resultMessageStruct.setCode(MessageExceptionEnum.PARAMETER_ERROR.getCode());
            resultMessageStruct.setMessage(MessageExceptionEnum.PARAMETER_ERROR.getMsg());
            log.error("发送短信参数错误,参数内容为:{}", commonMessage.toString());
            return resultMessageStruct;
        }
        return messageService.sendMessage(commonMessage);
    }

}
